package com.xuan.temper.controller;

import com.xuan.temper.entity.Data;
import com.xuan.temper.entity.ReturnData;
import com.xuan.temper.service.CodeTempService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @创建人： king xuan
 * @创建时间：2020/8/21
 * @描述：
 */
@RestController
@RequestMapping("/code")
public class CodeTemplController {

    @Autowired
    private CodeTempService codeTempService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/temper")
    public ReturnData codeTempController(@RequestParam("startDate") String startDate,
                                               @RequestParam("endDate")String endDate,
                                               @RequestParam("stockCodes")String stockCodes,
                                                @RequestParam("type") String type) {


        String current[]={"pe_ttm.mcw","pe_ttm.ew","pe_ttm.ewpvo","pe_ttm.avg","pe_ttm.median","pb.mcw","pb.ew","pb.ewpvo","pb.avg","pb.median","dyr.mcw","dyr.ew","dyr.ewpvo","dyr.avg","dyr.median","mc","cp","cpc"};
        String three [] ={"pe_ttm.y3.mcw","pe_ttm.y3.ew","pe_ttm.y3.ewpvo","pe_ttm.y3.avg","pe_ttm.y3.median","pb.y3.mcw","pb.y3.ew","pb.y3.ewpvo","pb.y3.avg","pb.y3.median","dyr.mcw","dyr.ew","dyr.ewpvo","dyr.avg","dyr.median","mc","cp","cpc"};
        String five [] ={"pe_ttm.y5.mcw","pe_ttm.y5.ew","pe_ttm.y5.ewpvo","pe_ttm.y5.avg","pe_ttm.y5.median","pb.y5.mcw","pb.y5.ew","pb.y5.ewpvo","pb.y5.avg","pb.y5.median","dyr.mcw","dyr.ew","dyr.ewpvo","dyr.avg","dyr.median","mc","cp","cpc"};
        String ten [] ={"pe_ttm.y10.mcw","pe_ttm.y10.ew","pe_ttm.y10.ewpvo","pe_ttm.y10.avg","pe_ttm.y10.median","pb.y10.mcw","pb.y10.ew","pb.y10.ewpvo","pb.y10.avg","pb.y10.median","dyr.mcw","dyr.ew","dyr.ewpvo","dyr.avg","dyr.median","mc","cp","cpc"};
        String twenty [] ={"pe_ttm.y20.mcw","pe_ttm.y20.ew","pe_ttm.y20.ewpvo","pe_ttm.y20.avg","pe_ttm.y20.median","pb.y20.mcw","pb.y20.ew","pb.y20.ewpvo","pb.y20.avg","pb.y20.median","dyr.mcw","dyr.ew","dyr.ewpvo","dyr.avg","dyr.median","mc","cp","cpc"};
        String all [] ={"pe_ttm.fs.mcw","pe_ttm.fs.ew","pe_ttm.fs.ewpvo","pe_ttm.fs.avg","pe_ttm.fs.median","pb.fs.mcw","pb.fs.ew","pb.fs.ewpvo","pb.fs.avg","pb.fs.median","dyr.mcw","dyr.ew","dyr.ewpvo","dyr.avg","dyr.median","mc","cp","cpc"};

        String[]  combination=null;
        if (type.equals("default")){
            combination = (String[]) ArrayUtils.addAll(current);
        }else if (type.equals("y3")){
            combination = (String[]) ArrayUtils.addAll(current, three);
        }else if (type.equals("y5")){
            combination = (String[]) ArrayUtils.addAll(current, five);
        }else if (type.equals("y10")){
            combination = (String[]) ArrayUtils.addAll(current, ten);
        }else if (type.equals("y20")){
            combination = (String[]) ArrayUtils.addAll(current, twenty);
        }else if (type.equals("fs")){
            combination = (String[]) ArrayUtils.addAll(current, all);
        }else if (type.equals("")){

        }

        ValueOperations<String, Object> operations = redisTemplate.opsForValue();

        String key = startDate+endDate+stockCodes+type;

        ArrayList o = (ArrayList) operations.get(key);
        List Return =null;
        if ( o!=null && o.size()!=0) {
            Return= (List) o;
            System.out.println("从redis中获取"+"-----------------------------------");
        return new ReturnData(200,"从redis中获取",Return);

        }else {
            List<Data> data = codeTempService.codeTemp(startDate, endDate, stockCodes, combination);
            operations.set(key, data);
//          operations.set(key, data,24, TimeUnit.HOURS);
            Return=data;
        }
        System.out.println("从api中获取");
        return new ReturnData(200,"从api中获取",Return);
    }

}
